<?php
namespace Index\Controller;
use think\Controller;

class ProductsController extends Controller{

	//产品详情
	public function view(){

	
		$id = I('get.id');  //id
		$p  = M('products');
		$getProductInfo = $p->find($id);
		$getProductInfo['details'] = html_entity_decode($getProductInfo['details']);
		$getProductInfo['endetails'] = html_entity_decode($getProductInfo['endetails']);



		//web config
		$getInfo = M('web_config');
		$getConf = $getInfo->find(1);
		$getConf['logo_img'] = '/Public/Uploads/'.$getConf['logo_img'];
		
		//echo 		$getConf['logo_img'];die;

		//get category
		$cate = M('category');
		$getTopCate = $cate->where(['pid'=>0])->order('sort asc')->select();

		foreach($getTopCate as &$v){
			$v['sub'] = $cate->where(['pid'=>$v['id']])->select();
		}
		
		$this->assign([
			
			'getProductInfo' => $getProductInfo,
			'getConf'  =>  $getConf, 
			'getCate' => $getTopCate, 
			
			'getInfo' => $getInfo,

		]);




		$this->display();
	}


	//列出所有产品
	public function productsAll(){

		$p = M('products');

		
		
		$pageSize 	= I('post.pageSize') ? I('post.pageSize') : 25;   //页尺寸
		$pageNo 	= I('post.pageNo') ? I('post.pageNo') : 1;		//页码

		$limit = ''.($pageSize*$pageNo-$pageSize).','.$pageSize;   //设置分页查询的 limit语句
		$getProductList = $p->limit($limit)->select();
		$getcount = $p->field('count(*) as count')->find();  //总记录数
		$countpage = ceil($getcount['count']/$pageSize);   //数数总共有多少页

		foreach($getProductList as &$v){  //图片路径需要拼装
			$v['product_img'] = '/Public/product_img'.$v['product_img'];
		}
		// dump(I('post.'));
		


		//web config
		$getInfo = M('web_config');
		$getConf = $getInfo->find(1);
		$getConf['logo_img'] = '/Public/Uploads/'.$getConf['logo_img'];
		
		//echo 		$getConf['logo_img'];die;

		//get category
		$cate = M('category');
		$getTopCate = $cate->where(['pid'=>0])->order('sort asc')->select();

		foreach($getTopCate as &$v){
			$v['sub'] = $cate->where(['pid'=>$v['id']])->select();
		}
		

		/*页码信息拼装数组*/
		$count=[];  //初始化数组
		$count['pageSize'] = $pageSize;
		$count['pageNo'] = $pageNo;
		$count['count'] = $getcount['count'];
		$count['countpage'] = $countpage;

		// dump($count);die;

		/*前端模板赋值*/
		$this->assign([
			
			'getProductList' => $getProductList,
			'getConf'  =>  $getConf, 
			'getCate' => $getTopCate, 
			'getInfo' => $getInfo,

			/*页码信息*/
			'count'   => $count,

		]);




		$this->display();
	}


	//只显示对应类目的商品
	public function products(){

	
		$id = I('get.cateid');  //id
		$p = M('products');

		$pageSize 	= I('post.pageSize') ? I('post.pageSize') : 25;   //页尺寸
		$pageNo 	= I('post.pageNo') ? I('post.pageNo') : 1;		//页码

		$limit = ''.($pageSize*$pageNo-$pageSize).','.$pageSize;   //设置分页查询的 limit语句
		$getProductList = $p->where(['cateid'=>$id])->limit($limit)->select();
		$getcount = $p->field('count(*) as count')->where(['cateid'=>$id])->find();  //总记录数
		$countpage = ceil($getcount['count']/$pageSize);   //数数总共有多少页

		/*页码信息拼装数组*/
		$count=[];  //初始化数组
		$count['pageSize'] = $pageSize;
		$count['pageNo'] = $pageNo;
		$count['count'] = $getcount['count'];
		$count['countpage'] = $countpage;

		
		foreach($getProductList as &$v){
			$v['product_img'] = '/Public/product_img'.$v['product_img'];
		}


		//web config
		$getInfo = M('web_config');
		$getConf = $getInfo->find(1);
		$getConf['logo_img'] = '/Public/Uploads/'.$getConf['logo_img'];
		
		
		//get category
		$cate = M('category');
		$getTopCate = $cate->where(['pid'=>0])->order('sort asc')->select();

		foreach($getTopCate as &$v){
			$v['sub'] = $cate->where(['pid'=>$v['id']])->select();
		}
		
		$this->assign([
			
			'getProductList' => $getProductList,
			'getConf'  =>  $getConf, 
			'getCate' => $getTopCate, 
			'getInfo' => $getInfo,
			'count'   => $count,  //页码

		]);


		$this->display();
	}






}
